package com.government.tag.service.impl;

import java.sql.Wrapper;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.government.common.core.domain.BaseTreeVo;
import com.government.common.exception.ServiceException;
import com.government.common.utils.DateUtils;
import com.government.common.utils.TreeUtils;
import com.government.tag.bo.TagBO;
import com.government.tag.domain.TagType;
import com.government.tag.mapper.TagTypeMapper;
import com.government.tag.vo.TagTreeVO;
import com.government.tag.vo.TagVO;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.government.tag.mapper.TagMapper;
import com.government.tag.domain.Tag;
import com.government.tag.service.ITagService;

/**
 * 标签信息Service业务层处理
 *
 * @author system
 * @date 2023-09-13
 */
@Service
public class TagServiceImpl implements ITagService {
    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private TagTypeMapper tagTypeMapper;

    /**
     * 查询标签信息
     *
     * @param tagId 标签信息主键
     * @return 标签信息
     */
    @Override
    public Tag selectTagByTagId(Long tagId) {
        return tagMapper.selectById(tagId);
    }

    /**
     * 查询标签信息列表
     *
     * @param tag 标签信息
     * @return 标签信息
     */
    @Override
    public List<TagVO> selectTagList(Tag tag) {
        return tagMapper.selectTagList(tag);
    }

    /**
     * 新增标签信息
     *
     * @param tag 标签信息
     * @return 结果
     */
    @Override
    public int insertTag(Tag tag) {
        int count = tagMapper.selectCount(Wrappers.<Tag>lambdaQuery().eq(Tag::getName, tag.getName()));
        ServiceException.throwExOnCondition(count > 0, "该标签名已存在");
        tag.setCreateTime(DateUtils.getNowDate());
        return tagMapper.insert(tag);
    }

    /**
     * 修改标签信息
     *
     * @param tag 标签信息
     * @return 结果
     */
    @Override
    public int updateTag(Tag tag) {
        int count = tagMapper.selectCount(Wrappers.<Tag>lambdaQuery()
                .eq(Tag::getName, tag.getName())
                .ne(Tag::getId, tag.getId()));
        ServiceException.throwExOnCondition(count > 0, "该标签名已存在");
        tag.setUpdateTime(DateUtils.getNowDate());
        return tagMapper.updateById(tag);
    }

    /**
     * 批量删除标签信息
     *
     * @param tagIds 需要删除的标签信息主键
     * @return 结果
     */
    @Override
    public int deleteTagByTagIds(Long[] tagIds) {
        return tagMapper.deleteTagByTagIds(tagIds);
    }

    /**
     * 删除标签信息信息
     *
     * @param tagId 标签信息主键
     * @return 结果
     */
    @Override
    public int deleteTagByTagId(Long tagId) {
        Tag tag = tagMapper.selectById(tagId);
        tag.setIsDeleted("1");
        return tagMapper.updateById(tag);
    }

    /**
     * 查询标签树结构信息
     *
     * @return 部门树信息集合
     */
    @Override
    public List<BaseTreeVo> tagSelect(TagBO tag) {

        List<Tag> tagList = tagMapper.selectList(Wrappers.<Tag>lambdaQuery().eq(Tag::getIsDeleted, "0").orderByAsc(Tag::getSort));
        if (CollectionUtils.isEmpty(tagList)) {

            return null;
        }
        HashMap<Long, List<BaseTreeVo>> allNodeMap = new HashMap<>();

        for (Tag item : tagList) {
            Long parentId = Objects.isNull(item.getParentId()) ? 0 : item.getParentId();
            List<BaseTreeVo> nodeList = allNodeMap.get(parentId);
            if (CollectionUtils.isEmpty(nodeList)) {

                nodeList = Lists.newArrayList();
            }
            BaseTreeVo tagTypeTreeVO = new BaseTreeVo();
            tagTypeTreeVO.setId(String.valueOf(item.getId()));
            tagTypeTreeVO.setValue(item.getId());
            tagTypeTreeVO.setLabel(item.getName());
            nodeList.add(tagTypeTreeVO);
            allNodeMap.put(parentId, nodeList);
        }
        List<BaseTreeVo> rootNode = allNodeMap.get(0L);

        return TreeUtils.buildTree(rootNode, allNodeMap);
    }
}
